#include <dbg.h>
#include <gtest/gtest.h>

#include <numeric>
using namespace std;

#ifdef Precision
class Solution {
public:
    long long interchangeableRectangles(vector<vector<int>>& rectangles) {
        unordered_map<pair<int, int>, int, pair_hash> countMap;

        long long res = 0;
        for (auto& rectangle : rectangles) {
            int x = rectangle[0];
            int y = rectangle[1];
            int g = gcd(x, y);  // 计算最大公约数

            pair<int, int> ratio = {x / g, y / g};  // 归一化比值
            res += countMap[ratio]++;
        }

        return res;
    }

private:
    struct pair_hash {  // 自定义哈希函数，支持 std::pair 作为 unordered_map 的
                        // key
        size_t operator()(const pair<int, int>& p) const {
            return hash<int>()(p.first) ^ (hash<int>()(p.second) << 1);
        }
    };
};
#else

class Solution {
public:
    long long interchangeableRectangles(vector<vector<int>>& rectangles) {
        unordered_map<double, int> countMap;

        long long res = 0;
        for (auto& rectangle : rectangles) {
            int x = rectangle[0];
            int y = rectangle[1];

            double ratio = static_cast<double>(x) / y;  // 计算比值
            dbg(x, y, ratio);
            res += countMap
                [ratio]++;  // 累加当前元素的出现次数，并更新映射表中的计数
        }
        return res;
    }
};
#endif

TEST(TEST1_interchangeableRectangles_Test, interchangeableRectangles) {
    Solution solution;

    vector<vector<int>> rectangles{{4, 8}, {3, 6}, {10, 20}, {15, 30}};
    EXPECT_EQ(solution.interchangeableRectangles(rectangles), 6);
}

TEST(TEST2_interchangeableRectangles_Test, interchangeableRectangles) {
    Solution solution;

    vector<vector<int>> rectangles{{4, 5}, {7, 8}};
    EXPECT_EQ(solution.interchangeableRectangles(rectangles), 0);
}

int main(int argc, char** argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}